# -*- coding: utf-8 -*-
# @Time : 2023/12/26 22:49
# @Author : Lpb
# @File : order_server.py
from flask import Flask, request, jsonify, redis
import json
# import redis

app = Flask(__name__)
db = []

redis_conn = redis.Redis(
    host='127.0.0.1',
    port=6379,
    db=8,
    password='8948lulu'

)


@app.route('/create_order', methods=['post'])
def create_order():
    data = request.json
    order_id = data['order_id']
    for order in db:
        if order_id == order['order_id']:
            return jsonify({'msg': 'order already exist'}), 403
    order_msg = {'order_id': order_id, 'status': '未支付', 'address': 'shanghai'}
    db.append(order_msg)
    redis_conn.set(order_id, json.dumps(order_msg), ex=300)


@app.route('/quert_order', methods=['get'])
def get_order():
    data = request.args
    order_id = data['order_id']
    redis_res = redis_conn.get(order_id)
    if redis_res is not None:
        return jsonify(redis_res.decode('utf-8')), 200
    for order in db:
        if order_id == order['order_id']:
            return jsonify({'msg': 'order already exist'}), 403


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8999, debug=True)